package br.senai.dao;

import java.beans.Statement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.senai.entity.Professor;
import br.senai.util.Conexao;

public class ProfessorDAO {

	Connection connection;
	
	public ProfessorDAO(){
		connection = Conexao.getConnection();
	}

	public void adicionarProfessor(Professor professor) {
		Conexao conn = new Conexao();
		try {
			Statement stmt = (Statement) conn.getConn().createStatement();
			PreparedStatement ps = connection
					.prepareStatement("insert into professor (nome, telefone , cpf , endereco) values ("
							+ professor.getNome()
							+ ","
							+ professor.getTelefone()
							+ ", "
							+ professor.getCpf()
							+ ", "
							+ professor.getEndereco() + ");");
			ps.execute();
			ps.close();

		} catch (Exception e) {
			e.printStackTrace();
	}
	}

	public Professor buscaProfessor(Long id) throws SQLException {
		Professor p = new Professor();
		try {
			PreparedStatement stmt = connection.prepareStatement("select * from professor where idprofessor = ?");
			stmt.setLong(1, id);
			ResultSet rs = stmt.executeQuery();
			while(rs.next()){
			p.setId_professor(rs.getLong("idprofessor"));
			p.setNome(rs.getString("nome"));
			p.setEmail(rs.getString("email"));
			p.setCpf(rs.getString("cpf"));
			p.setEndereco(rs.getString("endereco"));
			p.setTelefone(rs.getString("telefone"));
			
			}
			rs.close();
			stmt.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return p;
	}

	public List<Professor> listarProfessor() {
		List<Professor> professor = new ArrayList<Professor>();
		try {
			PreparedStatement stmt = connection.prepareStatement("select * from professor");
			ResultSet rs = stmt.executeQuery();
			while(rs.next()){
				Professor p = new Professor();
				p.setId_professor(rs.getLong("idprofessor"));
				p.setNome(rs.getString("nome"));
				p.setEmail(rs.getString("email"));
				p.setCpf(rs.getString("cpf"));
				p.setEndereco(rs.getString("endereco"));
				p.setTelefone(rs.getString("telefone"));
				professor.add(p);
				
			}
			
			stmt.close();
			rs.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return professor;
	}

	public void editarProfessor(Professor professor) {
		try {
			PreparedStatement st = connection.prepareStatement("update professor set cpf=?, nome=?, telefone=?, endereco=? where id=?");
			st.setString(1, professor.getCpf());
			st.setString(2, professor.getNome());
			st.setString(3, professor.getTelefone());
			st.setString(4, professor.getEndereco());
			st.setLong(5, professor.getId_professor());

			st.execute();
			st.close();

			System.out.println("Contato editado!");
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	public void removerProfessor(Long id) {
		String sql = "delete from professor where idprofessor = " + id;
		try {
			PreparedStatement stmt = connection.prepareStatement(sql);
			stmt.execute();
			stmt.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
}
